package net.technics;

import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Users;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.hibernate.Query;
import org.hibernate.Session;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class Login {

	protected Shell			shlBomAdministrateur;
	private Text			text_login;
	private Text			text_mdp;
	private static Users	userSession;

	public static Users getUserSession() {
		return userSession;
	}

	public static void setUserSession(Users userSession) {
		Login.userSession = userSession;
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Login window = new Login();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlBomAdministrateur.open();
		shlBomAdministrateur.layout();
		while (!shlBomAdministrateur.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlBomAdministrateur = new Shell();
		shlBomAdministrateur.setImage(null);
		shlBomAdministrateur.setSize(450, 300);
		shlBomAdministrateur.setText("Bom Administrateur");
		shlBomAdministrateur.setLayout(null);

		Group grpAuthentification = new Group(shlBomAdministrateur, SWT.NONE);
		grpAuthentification.setText("Authentification");
		grpAuthentification.setBounds(26, 30, 336, 200);

		CLabel lblpassword = new CLabel(grpAuthentification, SWT.NONE);
		lblpassword.setBounds(42, 93, 76, 26);
		lblpassword.setText("Password");

		CLabel lbllogin = new CLabel(grpAuthentification, SWT.NONE);
		lbllogin.setBounds(42, 42, 76, 26);
		lbllogin.setText("Login");

		text_login = new Text(grpAuthentification, SWT.BORDER);
		text_login.setBounds(169, 42, 135, 26);

		text_mdp = new Text(grpAuthentification, SWT.BORDER | SWT.PASSWORD);
		text_mdp.setBounds(169, 93, 135, 26);

		/* Bouton Valider */
		Button btnValider = new Button(grpAuthentification, SWT.NONE);
		btnValider.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String login = text_login.getText();
				String mdp = text_mdp.getText();

				if (login == "" && mdp == "") {
					if (login == "") {
						MessageBox messageBox = new MessageBox(shlBomAdministrateur, SWT.OK | SWT.ICON_WARNING);
						messageBox.setMessage("Veuillez remplir le Login");
						messageBox.open();
					} else if (mdp == "") {
						MessageBox messageBox = new MessageBox(shlBomAdministrateur, SWT.OK | SWT.ICON_WARNING);
						messageBox.setMessage("Veuillez remplir le mot de passe");
						messageBox.open();
					}
				} else {
					Session session = HibernateUtil.getSession();
					Query query = session.createQuery("FROM Users WHERE username =:Rusername");
					query.setParameter("Rusername", login);
					List<Users> users = query.list();
					for (Users visiteur : users) {
						userSession = visiteur;
					}
					Accueil accueil = new Accueil();
					shlBomAdministrateur.close();
					accueil.open();
				}

			}
		});
		btnValider.setBounds(140, 160, 90, 30);
		btnValider.setText("Valider");

		Button btnannuler = new Button(grpAuthentification, SWT.NONE);
		btnannuler.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shlBomAdministrateur.close();
			}
		});
		btnannuler.setBounds(236, 160, 90, 30);
		btnannuler.setText("Annuler");

		/* Bouton admin */
		Button btnAdmin = new Button(grpAuthentification, SWT.NONE);
		btnAdmin.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_login.setText("admin");
				text_mdp.setText("admin");
			}
		});
		btnAdmin.setBounds(28, 160, 90, 30);
		btnAdmin.setText("Admin");

		Composite composite = new Composite(shlBomAdministrateur, SWT.NONE);
		composite.setBounds(0, 0, 432, 253);

	}
}
